home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb / sprite / RCS / sparc-dep.c,v < prev    next >
Encoding:
Text File  |  1991-09-26  |  27.8 KB  |  1,088 lines

  1. head     1.3;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.3
  10. date     91.09.26.12.07.04;  author jhh;  state Exp;
  11. branches ;
  12. next     1.2;
  13.  
  14. 1.2
  15. date     90.12.09.22.36.13;  author rab;  state Exp;
  16. branches ;
  17. next     1.1;
  18.  
  19. 1.1
  20. date     90.11.15.19.04.32;  author rab;  state Exp;
  21. branches ;
  22. next     ;
  23.  
  24.  
  25. desc
  26. @@
  27.  
  28.  
  29. 1.3
  30. log
  31. @didn't read Sprite core files properly
  32. @
  33. text
  34. @/* Machine-dependent code which would otherwise be in inflow.c and core.c,
  35.    for GDB, the GNU debugger.
  36.    Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc.
  37.    This code is for the sparc cpu.
  38.  
  39. This file is part of GDB.
  40.  
  41. GDB is free software; you can redistribute it and/or modify
  42. it under the terms of the GNU General Public License as published by
  43. the Free Software Foundation; either version 1, or (at your option)
  44. any later version.
  45.  
  46. GDB is distributed in the hope that it will be useful,
  47. but WITHOUT ANY WARRANTY; without even the implied warranty of
  48. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  49. GNU General Public License for more details.
  50.  
  51. You should have received a copy of the GNU General Public License
  52. along with GDB; see the file COPYING.  If not, write to
  53. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  54.  
  55. #include <stdio.h>
  56. #include "defs.h"
  57. #include "param.h"
  58. #include "frame.h"
  59. #include "inferior.h"
  60. #include "obstack.h"
  61.  
  62. #include <sys/param.h>
  63. #include <sys/dir.h>
  64. #include <sys/user.h>
  65. #include <signal.h>
  66. #include <sys/ioctl.h>
  67. #include <fcntl.h>
  68.  
  69. #include <sys/ptrace.h>
  70. #include <sun4.md/reg.h>
  71.  
  72. #include <a.out.h>
  73. #include <sys/file.h>
  74. #include <sys/stat.h>
  75. #include <sys/core.h>
  76.  
  77. extern int errno;
  78. extern int attach_flag;
  79.  
  80. typedef enum
  81. {
  82.   Error, not_branch, bicc, bicca, ba, baa, ticc, ta,
  83. } branch_type;
  84.  
  85. /* This function simply calls ptrace with the given arguments.  
  86.    It exists so that all calls to ptrace are isolated in this 
  87.    machine-dependent file. */
  88. int
  89. call_ptrace (request, pid, arg3, arg4)
  90.      int request, pid, arg3, arg4;
  91. {
  92.   return ptrace (request, pid, arg3, arg4);
  93. }
  94.  
  95. void
  96. kill_inferior ()
  97. {
  98.   if (remote_debugging)
  99.     return;
  100.   if (inferior_pid == 0)
  101.     return;
  102.   ptrace (8, inferior_pid, 0, 0);
  103.   wait (0);
  104.   inferior_died ();
  105. }
  106.  
  107. /* This is used when GDB is exiting.  It gives less chance of error.*/
  108.  
  109. void
  110. kill_inferior_fast ()
  111. {
  112.   if (remote_debugging)
  113.     return;
  114.   if (inferior_pid == 0)
  115.     return;
  116.   ptrace (8, inferior_pid, 0, 0);
  117.   wait (0);
  118. }
  119.  
  120. /* Simulate single-step ptrace call for sun4.  Code written by Gary
  121.    Beihl (beihl@@mcc.com).  */
  122.  
  123. /* 
  124.  * Duplicated from breakpoint.c because (at least for now) this is a
  125.  * machine dependent routine.
  126.  */
  127. static char break_insn[] = BREAKPOINT;
  128.  
  129. /* From infrun.c */
  130. extern int stop_after_trap, stop_after_attach;
  131.  
  132. static CORE_ADDR next_pc, npc4, target;
  133. static int brknpc4, brktrg;
  134. typedef char binsn_quantum[sizeof break_insn];
  135. static binsn_quantum break_mem[3];
  136.  
  137. /* Non-zero if we just simulated a single-step ptrace call.  This is
  138.    needed because we cannot remove the breakpoints in the inferior
  139.    process until after the `wait' in `wait_for_inferior'.  Used for
  140.    sun4. */
  141.  
  142. int one_stepped;
  143.  
  144. void
  145. single_step (signal)
  146.      int signal;
  147. {
  148.   branch_type br, isannulled();
  149.   CORE_ADDR pc;
  150.  
  151.   next_pc = read_register (NPC_REGNUM);
  152.   npc4 = next_pc + 4; /* branch not taken */
  153.  
  154.   if (!one_stepped)
  155.     {
  156.       /* Always set breakpoint for NPC.  */
  157.       read_memory (next_pc, break_mem[0], sizeof break_insn);
  158.       write_memory (next_pc, break_insn, sizeof break_insn);
  159.       /* printf ("set break at %x\n",next_pc); */
  160.  
  161.       pc = read_register (PC_REGNUM);
  162.       br = isannulled (pc, &target);
  163.       brknpc4 = brktrg = 0;
  164.  
  165.       if (br == bicca)
  166.     {
  167.       /* Conditional annulled branch will either end up at
  168.          npc (if taken) or at npc+4 (if not taken).
  169.          Trap npc+4.  */
  170.       brknpc4 = 1;
  171.       read_memory (npc4, break_mem[1], sizeof break_insn);
  172.       write_memory (npc4, break_insn, sizeof break_insn);
  173.     }
  174.       else if (br == baa && target != next_pc)
  175.     {
  176.       /* Unconditional annulled branch will always end up at
  177.          the target.  */
  178.       brktrg = 1;
  179.       read_memory (target, break_mem[2], sizeof break_insn);
  180.       write_memory (target, break_insn, sizeof break_insn);
  181.     }
  182.  
  183.       /* Let it go */
  184.       ptrace (7, inferior_pid, 1, signal);
  185.       one_stepped = 1;
  186.       return;
  187.     }
  188.   else
  189.     {
  190.       /* Remove breakpoints */
  191.       write_memory (next_pc, break_mem[0], sizeof break_insn);
  192.  
  193.       if (brknpc4)
  194.     {
  195.       write_memory (npc4, break_mem[1], sizeof break_insn);
  196.     }
  197.       if (brktrg)
  198.     {
  199.       write_memory (target, break_mem[2], sizeof break_insn);
  200.     }
  201.       one_stepped = 0;
  202.     }
  203. }
  204.  
  205. /* Resume execution of the inferior process.
  206.    If STEP is nonzero, single-step it.
  207.    If SIGNAL is nonzero, give it that signal.  */
  208.  
  209. void
  210. resume (step, signal)
  211.      int step;
  212.      int signal;
  213. {
  214.   errno = 0;
  215.   if (remote_debugging)
  216.     remote_resume (step, signal);
  217.   else
  218.     {
  219.       /* Sparc doesn't have single step on ptrace */
  220.       if (step)
  221.     single_step (signal);
  222.       else
  223.     ptrace (7, inferior_pid, 1, signal);
  224.       if (errno)
  225.     perror_with_name ("ptrace");
  226.     }
  227. }
  228.  
  229. #ifdef ATTACH_DETACH
  230.  
  231. /* Start debugging the process whose number is PID.  */
  232.  
  233. int
  234. attach (pid)
  235.      int pid;
  236. {
  237.   errno = 0;
  238.   ptrace (PTRACE_ATTACH, pid, 0, 0);
  239.   if (errno)
  240.     perror_with_name ("ptrace");
  241.   attach_flag = 1;
  242.   return pid;
  243. }
  244.  
  245. /* Stop debugging the process whose number is PID
  246.    and continue it with signal number SIGNAL.
  247.    SIGNAL = 0 means just continue it.  */
  248.  
  249. void
  250. detach (signal)
  251.      int signal;
  252. {
  253.   errno = 0;
  254.   ptrace (PTRACE_DETACH, inferior_pid, 1, signal);
  255.   if (errno)
  256.     perror_with_name ("ptrace");
  257.   attach_flag = 0;
  258. }
  259. #endif /* ATTACH_DETACH */
  260.  
  261. void
  262. fetch_inferior_registers ()
  263. {
  264.   struct regs inferior_registers;
  265.   struct fp_status inferior_fp_registers;
  266.   extern char registers[];
  267.   int cwp;
  268.   struct rwindow local_and_ins;
  269.  
  270.   if (remote_debugging)
  271.     remote_fetch_registers (registers);
  272.   else
  273.     {
  274.       ptrace (PTRACE_GETREGS, inferior_pid, &inferior_registers);
  275.       ptrace (PTRACE_GETFPREGS, inferior_pid, &inferior_fp_registers);
  276.  
  277.       registers[REGISTER_BYTE (0)] = 0;
  278.       bcopy (&inferior_registers.r_g1, ®isters[REGISTER_BYTE (1)], 15 * 4);
  279.       bcopy (&inferior_fp_registers, ®isters[REGISTER_BYTE (FP0_REGNUM)],
  280.          sizeof inferior_fp_registers.fpu_fr);
  281.       *(int *)®isters[REGISTER_BYTE (PS_REGNUM)] = inferior_registers.r_ps; 
  282.       *(int *)®isters[REGISTER_BYTE (PC_REGNUM)] = inferior_registers.r_pc;
  283.       *(int *)®isters[REGISTER_BYTE (NPC_REGNUM)] = inferior_registers.r_npc;
  284.       *(int *)®isters[REGISTER_BYTE (Y_REGNUM)] = inferior_registers.r_y;
  285. /*      *(int *)®isters[REGISTER_BYTE (RP_REGNUM)] =
  286.              inferior_registers.r_o7 + 8;
  287.       bcopy (&inferior_fp_registers.Fpu_fsr,
  288.          ®isters[REGISTER_BYTE (FPS_REGNUM)],
  289.          sizeof (FPU_FSR_TYPE)); */
  290.  
  291.       read_inferior_memory (inferior_registers.r_sp,
  292.                 ®isters[REGISTER_BYTE (16)],
  293.                 16*4);
  294.     }
  295. }
  296.  
  297. /* Store our register values back into the inferior.
  298.    If REGNO is -1, do this for all registers.
  299.    Otherwise, REGNO specifies which register (so we can save time).  */
  300.  
  301. void
  302. store_inferior_registers (regno)
  303.      int regno;
  304. {
  305.   struct regs inferior_registers;
  306.   struct fp_status inferior_fp_registers;
  307.   extern char registers[];
  308.  
  309.   if (remote_debugging)
  310.     remote_store_registers (registers);
  311.   else
  312.     {
  313.       int in_regs = 1, in_fpregs = 1, in_fparegs, in_cpregs = 1;
  314.   
  315.       if (regno >= 0)
  316.     if (FP0_REGNUM <= regno && regno <= FP0_REGNUM + 32)
  317.       in_regs = 0;
  318.     else
  319.       in_fpregs = 0;
  320.  
  321.       if (in_regs)
  322.     {
  323.       bcopy (®isters[REGISTER_BYTE (1)],
  324.          &inferior_registers.r_g1, 15 * 4);
  325.  
  326.       inferior_registers.r_ps =
  327.         *(int *)®isters[REGISTER_BYTE (PS_REGNUM)];
  328.       inferior_registers.r_pc =
  329.         *(int *)®isters[REGISTER_BYTE (PC_REGNUM)];
  330.       inferior_registers.r_npc =
  331.         *(int *)®isters[REGISTER_BYTE (NPC_REGNUM)];
  332.       inferior_registers.r_y =
  333.         *(int *)®isters[REGISTER_BYTE (Y_REGNUM)];
  334.  
  335.       write_inferior_memory (*(int *)®isters[REGISTER_BYTE (SP_REGNUM)],
  336.                  ®isters[REGISTER_BYTE (16)],
  337.                  16*4);
  338.     }
  339.       if (in_fpregs)
  340.     {
  341.       bcopy (®isters[REGISTER_BYTE (FP0_REGNUM)],
  342.          &inferior_fp_registers,
  343.          sizeof inferior_fp_registers.fpu_fr);
  344.  
  345.   /*      bcopy (®isters[REGISTER_BYTE (FPS_REGNUM)],
  346.              &inferior_fp_registers.Fpu_fsr,
  347.          sizeof (FPU_FSR_TYPE));
  348.   ****/
  349.     }
  350.  
  351.       if (in_regs)
  352.     ptrace (PTRACE_SETREGS, inferior_pid, &inferior_registers);
  353.       if (in_fpregs)
  354.     ptrace (PTRACE_SETFPREGS, inferior_pid, &inferior_fp_registers);
  355.     }
  356. }
  357.  
  358. /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
  359.    in the NEW_SUN_PTRACE case.
  360.    It ought to be straightforward.  But it appears that writing did
  361.    not write the data that I specified.  I cannot understand where
  362.    it got the data that it actually did write.  */
  363.  
  364. /* Copy LEN bytes from inferior's memory starting at MEMADDR
  365.    to debugger memory starting at MYADDR. 
  366.    On failure (cannot read from inferior, usually because address is out
  367.    of bounds) returns the value of errno. */
  368.  
  369. int
  370. read_inferior_memory (memaddr, myaddr, len)
  371.      CORE_ADDR memaddr;
  372.      char *myaddr;
  373.      int len;
  374. {
  375.   register int i;
  376.   /* Round starting address down to longword boundary.  */
  377.   register CORE_ADDR addr = memaddr & - sizeof (int);
  378.   /* Round ending address up; get number of longwords that makes.  */
  379.   register int count
  380.     = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
  381.   /* Allocate buffer of that many longwords.  */
  382.   register int *buffer = (int *) alloca (count * sizeof (int));
  383.   extern int errno;
  384.  
  385.   /* Read all the longwords */
  386.   for (i = 0; i < count; i++, addr += sizeof (int))
  387.     {
  388.       errno = 0;
  389.       if (remote_debugging)
  390.     buffer[i] = remote_fetch_word (addr);
  391.       else
  392.     buffer[i] = ptrace (1, inferior_pid, addr, 0);
  393.       if (errno)
  394.     return errno;
  395.     }
  396.  
  397.   /* Copy appropriate bytes out of the buffer.  */
  398.   bcopy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
  399.   return 0;
  400. }
  401.  
  402. /* Copy LEN bytes of data from debugger memory at MYADDR
  403.    to inferior's memory at MEMADDR.
  404.    On failure (cannot write the inferior)
  405.    returns the value of errno.  */
  406.  
  407. int
  408. write_inferior_memory (memaddr, myaddr, len)
  409.      CORE_ADDR memaddr;
  410.      char *myaddr;
  411.      int len;
  412. {
  413.   register int i;
  414.   /* Round starting address down to longword boundary.  */
  415.   register CORE_ADDR addr = memaddr & - sizeof (int);
  416.   /* Round ending address up; get number of longwords that makes.  */
  417.   register int count
  418.     = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
  419.   /* Allocate buffer of that many longwords.  */
  420.   register int *buffer = (int *) alloca (count * sizeof (int));
  421.   extern int errno;
  422.  
  423.   /* Fill start and end extra bytes of buffer with existing memory data.  */
  424.  
  425.   if (remote_debugging)
  426.     buffer[0] = remote_fetch_word (addr);
  427.   else
  428.     buffer[0] = ptrace (1, inferior_pid, addr, 0);
  429.  
  430.   if (count > 1)
  431.     {
  432.       if (remote_debugging)
  433.     buffer[count - 1]
  434.       = remote_fetch_word (addr + (count - 1) * sizeof (int));
  435.       else
  436.     buffer[count - 1]
  437.       = ptrace (1, inferior_pid,
  438.             addr + (count - 1) * sizeof (int), 0);
  439.     }
  440.  
  441.   /* Copy data to be written over corresponding part of buffer */
  442.  
  443.   bcopy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
  444.  
  445.   /* Write the entire buffer.  */
  446.  
  447.   for (i = 0; i < count; i++, addr += sizeof (int))
  448.     {
  449.       errno = 0;
  450.       if (remote_debugging)
  451.     remote_store_word (addr, buffer[i]);
  452.       else
  453.     ptrace (4, inferior_pid, addr, buffer[i]);
  454.       if (errno)
  455.     return errno;
  456.     }
  457.  
  458.   return 0;
  459. }
  460.  
  461.  
  462. /* Machine-dependent code which would otherwise be in core.c */
  463. /* Work with core dump and executable files, for GDB. */
  464.  
  465. #ifndef N_TXTADDR
  466. #define N_TXTADDR(hdr) 0
  467. #endif /* no N_TXTADDR */
  468.  
  469. #ifndef N_DATADDR
  470. #define N_DATADDR(hdr) hdr.a_text
  471. #endif /* no N_DATADDR */
  472.  
  473. /* Non-zero if this is an object (.o) file, rather than an executable.
  474.    Distinguishing between the two is rarely necessary (and seems like
  475.    a hack, but there is no other way to get the text and data
  476.    addresses--N_TXTADDR should probably take care of
  477.    this, but it doesn't).  */
  478. /* This definition will not work
  479.    if someone decides to make ld preserve relocation info.  */
  480. #define IS_OBJECT_FILE(hdr) (hdr.a_trsize != 0)
  481.  
  482. /* Make COFF and non-COFF names for things a little more compatible
  483.    to reduce conditionals later.  */
  484.  
  485. #ifdef COFF_FORMAT
  486. #define a_magic magic
  487. #endif
  488.  
  489. #ifndef COFF_FORMAT
  490. #ifndef AOUTHDR
  491. #define AOUTHDR struct exec
  492. #endif
  493. #endif
  494.  
  495. extern char *sys_siglist[];
  496.  
  497. /* Hook for `exec_file_command' command to call.  */
  498.  
  499. extern void (*exec_file_display_hook) ();
  500.    
  501. /* File names of core file and executable file.  */
  502.  
  503. extern char *corefile;
  504. extern char *execfile;
  505.  
  506. /* Descriptors on which core file and executable file are open.
  507.    Note that the execchan is closed when an inferior is created
  508.    and reopened if the inferior dies or is killed.  */
  509.  
  510. extern int corechan;
  511. extern int execchan;
  512.  
  513. /* Last modification time of executable file.
  514.    Also used in source.c to compare against mtime of a source file.  */
  515.  
  516. extern int exec_mtime;
  517.  
  518. /* Virtual addresses of bounds of the two areas of memory in the core file.  */
  519.  
  520. extern CORE_ADDR data_start;
  521. extern CORE_ADDR data_end;
  522. extern CORE_ADDR stack_start;
  523. extern CORE_ADDR stack_end;
  524.  
  525. /* Virtual addresses of bounds of two areas of memory in the exec file.
  526.    Note that the data area in the exec file is used only when there is no core file.  */
  527.  
  528. extern CORE_ADDR text_start;
  529. extern CORE_ADDR text_end;
  530.  
  531. extern CORE_ADDR exec_data_start;
  532. extern CORE_ADDR exec_data_end;
  533.  
  534. /* Address in executable file of start of text area data.  */
  535.  
  536. extern int text_offset;
  537.  
  538. /* Address in executable file of start of data area data.  */
  539.  
  540. extern int exec_data_offset;
  541.  
  542. /* Address in core file of start of data area data.  */
  543.  
  544. extern int data_offset;
  545.  
  546. /* Address in core file of start of stack area data.  */
  547.  
  548. extern int stack_offset;
  549.   
  550. #ifdef COFF_FORMAT
  551. /* various coff data structures */
  552.  
  553. extern FILHDR file_hdr;
  554. extern SCNHDR text_hdr;
  555. extern SCNHDR data_hdr;
  556.  
  557. #endif /* not COFF_FORMAT */
  558.  
  559. /* a.out header saved in core file.  */
  560.   
  561. extern AOUTHDR core_aouthdr;
  562.  
  563. /* a.out header of exec file.  */
  564.  
  565. extern AOUTHDR exec_aouthdr;
  566.  
  567. extern void validate_files ();
  568.  
  569. void
  570. core_file_command (filename, from_tty)
  571.      char *filename;
  572.      int from_tty;
  573. {
  574.   int val;
  575.   extern char registers[];
  576.  
  577.   /* Discard all vestiges of any previous core file
  578.      and mark data and stack spaces as empty.  */
  579.  
  580.   if (corefile)
  581.     free (corefile);
  582.   corefile = 0;
  583.  
  584.   if (corechan >= 0)
  585.     close (corechan);
  586.   corechan = -1;
  587.  
  588.   data_start = 0;
  589.   data_end = 0;
  590.   stack_start = STACK_END_ADDR;
  591.   stack_end = STACK_END_ADDR;
  592.  
  593.   /* Now, if a new core file was specified, open it and digest it.  */
  594.  
  595.   if (filename)
  596.     {
  597.       filename = tilde_expand (filename);
  598.       make_cleanup (free, filename);
  599.       
  600.       if (have_inferior_p ())
  601.     error ("To look at a core file, you must kill the inferior with \"kill\".");
  602.       corechan = open (filename, O_RDONLY, 0);
  603.       if (corechan < 0)
  604.     perror_with_name (filename);
  605.  
  606.       {
  607.     struct core corestr;
  608.  
  609.     val = myread (corechan, &corestr, sizeof corestr);
  610.     if (val < 0)
  611.       perror_with_name (filename);
  612.     if (corestr.c_magic != CORE_MAGIC)
  613.       error ("\"%s\" does not appear to be a core dump file (magic 0x%x, expected 0x%x)",
  614.          filename, corestr.c_magic, (int) CORE_MAGIC);
  615.     else if (sizeof (struct core) != corestr.c_len)
  616.       error ("\"%s\" has an invalid struct core length (%d, expected %d)",
  617.          filename, corestr.c_len, (int) sizeof (struct core));
  618.  
  619.     data_start = exec_data_start;
  620.     data_end = data_start + corestr.c_dsize;
  621.     stack_start = stack_end - corestr.c_ssize;
  622.     data_offset = sizeof corestr;
  623.     stack_offset = sizeof corestr + corestr.c_dsize;
  624.  
  625.     /* G0 *always* holds 0.  */
  626.     *(int *)®isters[REGISTER_BYTE (0)] = 0;
  627.     /* The globals and output registers.  */
  628.  
  629.     bcopy (&corestr.c_regs.r_g1, ((int *) registers) + 1, 15 * 4);
  630.     *(int *)®isters[REGISTER_BYTE (PS_REGNUM)] = corestr.c_regs.r_ps;
  631.     *(int *)®isters[REGISTER_BYTE (PC_REGNUM)] = corestr.c_regs.r_pc;
  632.     *(int *)®isters[REGISTER_BYTE (NPC_REGNUM)] = corestr.c_regs.r_npc;
  633.     *(int *)®isters[REGISTER_BYTE (Y_REGNUM)] = corestr.c_regs.r_y;
  634.  
  635.     /* My best guess at where to get the locals and input
  636.        registers is exactly where they usually are, right above
  637.        the stack pointer.  If the core dump was caused by a bus
  638.        writing off the stack pointer (as is possible) then this
  639.        won't work, but it's worth the try. */
  640.     {
  641.       int sp;
  642.  
  643.       sp = *(int *)®isters[REGISTER_BYTE (SP_REGNUM)];
  644.       /*
  645.        * Sprite core files seem to be different from SunOS.
  646.        * The stack segment starts at the beginning of the page
  647.        * containing the stack pointer.  We can compute the stack
  648.        * start and end from this fact, rather than from the 
  649.        * STACK_END_ADDR constant.
  650.        */
  651. #ifdef sprite
  652.       stack_start = sp & ~(getpagesize() - 1);
  653.       stack_end = sp + corestr.c_ssize;
  654. #endif
  655.       lseek (corechan, sp - stack_start + stack_offset, L_SET);
  656.       if (16 * 4 != myread (corechan,
  657.                 ®isters[REGISTER_BYTE (16)],
  658.                 16 * 4))
  659.         /* fprintf so user can still use gdb */
  660.         fprintf (stderr, "Couldn't read input and local registers from core file\n");
  661.     }
  662.  
  663. #ifdef FPU
  664.     bcopy (corestr.c_fpu.fpu_regs,
  665.            ®isters[REGISTER_BYTE (FP0_REGNUM)],
  666.            sizeof corestr.c_fpu.fpu_regs);
  667.     bcopy (&corestr.c_fpu.fpu_fsr,
  668.            ®isters[REGISTER_BYTE (FPS_REGNUM)],
  669.            sizeof (FPU_FSR_TYPE));
  670. #endif
  671.  
  672.     bcopy (&corestr.c_aouthdr, &core_aouthdr, sizeof (struct exec));
  673.  
  674.     printf ("Core file is from \"%s\".\n", corestr.c_cmdname);
  675.     if (corestr.c_signo > 0)
  676.       printf ("Program terminated with signal %d, %s.\n",
  677.             corestr.c_signo,
  678.             corestr.c_signo < NSIG
  679.             ? sys_siglist[corestr.c_signo]
  680.             : "(undocumented)");
  681.       }
  682.       if (filename[0] == '/')
  683.     corefile = savestring (filename, strlen (filename));
  684.       else
  685.     {
  686.       corefile = concat (current_directory, "/", filename);
  687.     }
  688.  
  689.       set_current_frame ( create_new_frame (read_register (FP_REGNUM),
  690.                         read_pc ()));
  691.       select_frame (get_current_frame (), 0);
  692.       validate_files ();
  693.     }
  694.   else if (from_tty)
  695.     printf ("No core file now.\n");
  696. }
  697.  
  698. void
  699. exec_file_command (filename, from_tty)
  700.      char *filename;
  701.      int from_tty;
  702. {
  703.   int val;
  704.  
  705.   /* Eliminate all traces of old exec file.
  706.      Mark text segment as empty.  */
  707.  
  708.   if (execfile)
  709.     free (execfile);
  710.   execfile = 0;
  711.   data_start = 0;
  712.   data_end -= exec_data_start;
  713.   text_start = 0;
  714.   text_end = 0;
  715.   exec_data_start = 0;
  716.   exec_data_end = 0;
  717.   if (execchan >= 0)
  718.     close (execchan);
  719.   execchan = -1;
  720.  
  721.   /* Now open and digest the file the user requested, if any.  */
  722.  
  723.   if (filename)
  724.     {
  725.       filename = tilde_expand (filename);
  726.       make_cleanup (free, filename);
  727.       
  728.       execchan = openp (getenv ("PATH"), 1, filename, O_RDONLY, 0,
  729.             &execfile);
  730.       if (execchan < 0)
  731.     perror_with_name (filename);
  732.  
  733. #ifdef COFF_FORMAT
  734.       {
  735.     int aout_hdrsize;
  736.     int num_sections;
  737.  
  738.     if (read_file_hdr (execchan, &file_hdr) < 0)
  739.       error ("\"%s\": not in executable format.", execfile);
  740.  
  741.     aout_hdrsize = file_hdr.f_opthdr;
  742.     num_sections = file_hdr.f_nscns;
  743.  
  744.     if (read_aout_hdr (execchan, &exec_aouthdr, aout_hdrsize) < 0)
  745.       error ("\"%s\": can't read optional aouthdr", execfile);
  746.  
  747.     if (read_section_hdr (execchan, _TEXT, &text_hdr, num_sections,
  748.                   aout_hdrsize) < 0)
  749.       error ("\"%s\": can't read text section header", execfile);
  750.  
  751.     if (read_section_hdr (execchan, _DATA, &data_hdr, num_sections,
  752.                   aout_hdrsize) < 0)
  753.       error ("\"%s\": can't read data section header", execfile);
  754.  
  755.     text_start = exec_aouthdr.text_start;
  756.     text_end = text_start + exec_aouthdr.tsize;
  757.     text_offset = text_hdr.s_scnptr;
  758.     exec_data_start = exec_aouthdr.data_start;
  759.     exec_data_end = exec_data_start + exec_aouthdr.dsize;
  760.     exec_data_offset = data_hdr.s_scnptr;
  761.     data_start = exec_data_start;
  762.     data_end += exec_data_start;
  763.     exec_mtime = file_hdr.f_timdat;
  764.       }
  765. #else /* not COFF_FORMAT */
  766.       {
  767.     struct stat st_exec;
  768.     val = myread (execchan, &exec_aouthdr, sizeof (AOUTHDR));
  769.  
  770.     if (val < 0)
  771.       perror_with_name (filename);
  772.  
  773.     text_start =
  774.       IS_OBJECT_FILE (exec_aouthdr) ? 0 : N_TXTADDR (exec_aouthdr);
  775.         exec_data_start = IS_OBJECT_FILE (exec_aouthdr)
  776.       ? exec_aouthdr.a_text : N_DATADDR (exec_aouthdr);
  777.     text_offset = N_TXTOFF (exec_aouthdr);
  778.     exec_data_offset = N_TXTOFF (exec_aouthdr) + exec_aouthdr.a_text;
  779.  
  780.     text_end = text_start + exec_aouthdr.a_text;
  781.         exec_data_end = exec_data_start + exec_aouthdr.a_data;
  782.     data_start = exec_data_start;
  783.     data_end += exec_data_start;
  784.  
  785.     fstat (execchan, &st_exec);
  786.     exec_mtime = st_exec.st_mtime;
  787.       }
  788. #endif /* not COFF_FORMAT */
  789.  
  790.       validate_files ();
  791.     }
  792.   else if (from_tty)
  793.     printf ("No exec file now.\n");
  794.  
  795.   /* Tell display code (if any) about the changed file name.  */
  796.   if (exec_file_display_hook)
  797.     (*exec_file_display_hook) (filename);
  798. }
  799.  
  800. /*
  801.  * Find the pc saved in frame FRAME.  
  802.  */
  803. CORE_ADDR
  804. frame_saved_pc (frame)
  805.      FRAME frame;
  806. {
  807.   CORE_ADDR prev_pc;
  808.  
  809.   /* If it's at the bottom, the return value's stored in i7/rp */
  810.   if (get_current_frame () == frame)
  811.     prev_pc = GET_RWINDOW_REG (read_register (SP_REGNUM), rw_in[7]);
  812.   else
  813.     /* Wouldn't this always work?  This would allow this routine to
  814.        be completely a macro.  */
  815.     prev_pc = GET_RWINDOW_REG (frame->bottom, rw_in[7]);
  816.   
  817.   return PC_ADJUST (prev_pc);
  818. }
  819.  
  820. /*
  821.  * Since an individual frame in the frame cache is defined by two
  822.  * arguments (a frame pointer and a stack pointer), we need two
  823.  * arguments to get info for an arbitrary stack frame.  This routine
  824.  * takes two arguments and makes the cached frames look as if these
  825.  * two arguments defined a frame on the cache.  This allows the rest
  826.  * of info frame to extract the important arguments without
  827.  * difficulty. 
  828.  */
  829. FRAME
  830. setup_arbitrary_frame (frame, stack)
  831.      FRAME_ADDR frame, stack;
  832. {
  833.   struct frame_info *fci;
  834.   FRAME fid = create_new_frame (frame, 0);
  835.  
  836.   if (!fid)
  837.     fatal ("internal: create_new_frame returned invalid frame id");
  838.   
  839.   fid->bottom = stack;
  840.  
  841.   return fid;
  842. }
  843.  
  844. /* This code was written by Gary Beihl (beihl@@mcc.com).
  845.    It was modified by Michael Tiemann (tiemann@@corto.inria.fr).  */
  846.  
  847. struct command_line *get_breakpoint_commands ();
  848.  
  849. /*
  850.  * This routine appears to be passed a size by which to increase the
  851.  * stack.  It then executes a save instruction in the inferior to
  852.  * increase the stack by this amount.  Only the register window system
  853.  * should be affected by this; the program counter & etc. will not be.
  854.  *
  855.  * This instructions used for this purpose are:
  856.  *
  857.  *     sethi %hi(0x0),g1                    *
  858.  *     add g1,0x1ee0,g1                     *
  859.  *     save sp,g1,sp                        
  860.  *     sethi %hi(0x0),g1                    *
  861.  *     add g1,0x1ee0,g1                     *
  862.  *     t g0,0x1,o0
  863.  *     sethi %hi(0x0),g0                    (nop)
  864.  *
  865.  *  I presume that these set g1 to be the negative of the size, do a
  866.  * save (putting the stack pointer at sp - size) and restore the
  867.  * original contents of g1.  A * indicates that the actual value of
  868.  * the instruction is modified below.
  869.  */
  870. static int save_insn_opcodes[] = {
  871.   0x03000000, 0x82007ee0, 0x9de38001, 0x03000000,
  872.   0x82007ee0, 0x91d02001, 0x01000000 };
  873.  
  874. /* Neither do_save_insn or do_restore_insn save stack configuration
  875.    (since the stack is in an indeterminate state through the call to
  876.    each of them); that responsibility of the routine which calls them.  */
  877.  
  878. void
  879. do_save_insn (size)
  880.      int size;
  881. {
  882.   int g1 = read_register (1);
  883.   CORE_ADDR sp = read_register (SP_REGNUM);
  884.   CORE_ADDR pc = read_register (PC_REGNUM);
  885.   CORE_ADDR npc = read_register (NPC_REGNUM);
  886.   CORE_ADDR fake_pc = sp - sizeof (save_insn_opcodes);
  887.   struct inferior_status inf_status;
  888.  
  889.   save_inferior_status (&inf_status, 0); /* Don't restore stack info */
  890.   /*
  891.    * See above.
  892.    */
  893.   save_insn_opcodes[0] = 0x03000000 | ((-size >> 10) & 0x3fffff);
  894.   save_insn_opcodes[1] = 0x82006000 | (-size & 0x3ff);
  895.   save_insn_opcodes[3] = 0x03000000 | ((g1 >> 10) & 0x3fffff);
  896.   save_insn_opcodes[4] = 0x82006000 | (g1 & 0x3ff);
  897.   write_memory (fake_pc, save_insn_opcodes, sizeof (save_insn_opcodes));
  898.  
  899.   clear_proceed_status ();
  900.   stop_after_trap = 1;
  901.   proceed (fake_pc, 0, 0);
  902.  
  903.   write_register (PC_REGNUM, pc);
  904.   write_register (NPC_REGNUM, npc);
  905.   restore_inferior_status (&inf_status);
  906. }
  907.  
  908. /*
  909.  * This routine takes a program counter value.  It restores the
  910.  * register window system to the frame above the current one, and sets
  911.  * the pc and npc to the correct values.
  912.  */
  913.  
  914. /*    The following insns translate to:
  915.  
  916.      restore
  917.      t g0,0x1,o0
  918.      sethi %hi(0x0), g0    */
  919.  
  920. static int restore_insn_opcodes[] = { 0x81e80000, 0x91d02001, 0x01000000 };
  921.  
  922. void
  923. do_restore_insn (pc)
  924.      CORE_ADDR pc;
  925. {
  926.   CORE_ADDR sp = read_register (SP_REGNUM);
  927.   CORE_ADDR npc = pc + 4;
  928.   CORE_ADDR fake_pc = sp - sizeof (restore_insn_opcodes);
  929.   struct inferior_status inf_status;
  930.  
  931.   save_inferior_status (&inf_status, 0); /* Don't restore stack info */
  932.  
  933.   if (!pc)
  934.     abort();
  935.  
  936.   write_memory (fake_pc, restore_insn_opcodes, sizeof (restore_insn_opcodes));
  937.  
  938.   clear_proceed_status ();
  939.   stop_after_trap = 1;
  940.   proceed (fake_pc, 0, 0);
  941.  
  942.   write_register (PC_REGNUM, pc);
  943.   write_register (NPC_REGNUM, npc);
  944.   restore_inferior_status (&inf_status);
  945. }
  946.  
  947. /*
  948.  * This routine should be more specific in it's actions; making sure
  949.  * that it uses the same register in the initial prologue section.
  950.  */
  951. CORE_ADDR 
  952. skip_prologue (pc)
  953.      CORE_ADDR pc;
  954. {
  955.   union
  956.     {
  957.       unsigned long int code;
  958.       struct
  959.     {
  960.       unsigned int op:2;
  961.       unsigned int rd:5;
  962.       unsigned int op2:3;
  963.       unsigned int imm22:22;
  964.     } sethi;
  965.       struct
  966.     {
  967.       unsigned int op:2;
  968.       unsigned int rd:5;
  969.       unsigned int op3:6;
  970.       unsigned int rs1:5;
  971.       unsigned int i:1;
  972.       unsigned int simm13:13;
  973.     } add;
  974.       int i;
  975.     } x;
  976.   int dest = -1;
  977.  
  978.   x.i = read_memory_integer (pc, 4);
  979.  
  980.   /* Recognize the `sethi' insn and record its destination.  */
  981.   if (x.sethi.op == 0 && x.sethi.op2 == 4)
  982.     {
  983.       dest = x.sethi.rd;
  984.       pc += 4;
  985.       x.i = read_memory_integer (pc, 4);
  986.     }
  987.  
  988.   /* Recognize an add immediate value to register to either %g1 or
  989.      the destination register recorded above.  Actually, this might
  990.      well recognize several different arithmetic operations.  */
  991.   if (x.add.op == 2 && x.add.i && (x.add.rd == 1 || x.add.rd == dest))
  992.     {
  993.       pc += 4;
  994.       x.i = read_memory_integer (pc, 4);
  995.     }
  996.  
  997.   /* This recognizes any SAVE insn.  But why do the XOR and then
  998.      the compare?  That's identical to comparing against 60 (as long
  999.      as there isn't any sign extension).  */
  1000.   if (x.add.op == 2 && (x.add.op3 ^ 32) == 28)
  1001.     {
  1002.       pc += 4;
  1003.       x.i = read_memory_integer (pc, 4);
  1004.     }
  1005.  
  1006.   /* Now we need to recognize stores into the frame from the input
  1007.      registers.  This recognizes all non alternate stores of input
  1008.      register, into a location offset from the frame pointer.  */
  1009.   while (x.add.op == 3
  1010.      && (x.add.op3 & 0x3c) == 4 /* Store, non-alternate.  */
  1011.      && (x.add.rd & 0x18) == 0x18 /* Input register.  */
  1012.      && x.add.i        /* Immediate mode.  */
  1013.      && x.add.rs1 == 30    /* Off of frame pointer.  */
  1014.      /* Into reserved stack space.  */
  1015.      && x.add.simm13 >= 0x44
  1016.      && x.add.simm13 < 0x5b)
  1017.     {
  1018.       pc += 4;
  1019.       x.i = read_memory_integer (pc, 4);
  1020.     }
  1021.   return pc;
  1022. }
  1023.  
  1024. /* Check instruction at ADDR to see if it is an annulled branch.
  1025.    All other instructions will go to NPC or will trap.
  1026.    Set *TARGET if we find a canidate branch; set to zero if not. */
  1027.    
  1028. branch_type
  1029. isannulled (addr,  target)
  1030.      CORE_ADDR addr, *target;
  1031. {
  1032.   branch_type val = not_branch;
  1033.   long int offset;        /* Must be signed for sign-extend.  */
  1034.   union
  1035.     {
  1036.       unsigned long int code;
  1037.       struct
  1038.     {
  1039.       unsigned int op:2;
  1040.       unsigned int a:1;
  1041.       unsigned int cond:4;
  1042.       unsigned int op2:3;
  1043.       unsigned int disp22:22;
  1044.     } b;
  1045.     } insn;
  1046.  
  1047.   *target = 0;
  1048.   insn.code = read_memory_integer (addr, 4);
  1049.  
  1050.   if (insn.b.op == 0
  1051.       && (insn.b.op2 == 2 || insn.b.op2 == 6 || insn.b.op2 == 7))
  1052.     {
  1053.       if (insn.b.cond == 8)
  1054.     val = insn.b.a ? baa : ba;
  1055.       else
  1056.     val = insn.b.a ? bicca : bicc;
  1057.       offset = 4 * ((int) (insn.b.disp22 << 10) >> 10);
  1058.       *target = addr + offset;
  1059.     }
  1060.  
  1061.   return val;
  1062. }
  1063.  
  1064. @
  1065.  
  1066.  
  1067. 1.2
  1068. log
  1069. @Changes for Sprite. (Mike checking in for Bob.)
  1070. @
  1071. text
  1072. @d611 11
  1073. @
  1074.  
  1075.  
  1076. 1.1
  1077. log
  1078. @Initial revision
  1079. @
  1080. text
  1081. @d37 1
  1082. a37 1
  1083. #include <machine/reg.h>
  1084. d619 1
  1085. a622 1
  1086. #ifdef FPU
  1087. @
  1088.